﻿using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace Web7.ExceptionHandling
{
	/// <summary>
	/// 用于管理所有<see cref="IExceptionPublisher"/>的容器
	/// </summary>
	public static class ExceptionPublisherManager
	{
		/// <summary>
		/// 获取已实现（提供）的所有<see cref="IExceptionPublisher"/>实例
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public static ReadOnlyCollection<IExceptionPublisher> AllPublishers
		{
			get
			{
				// TODO: 注意 DI 容器多实例的取法
				//IDependencyResolver dependencyResolver = DependencyResolver.Current;
				//registeredPublishers = dependencyResolver.GetServices<IExceptionPublisher>().ToList();

				//registeredPublishers.Add(dependencyResolver.GetService<IExceptionPublisher>());

				return registeredPublishers.AsReadOnly();
			}
		}
		/// <summary>
		/// 向系统注册一个异常发布程序
		/// </summary>
		public static void RegisterPublisher(IExceptionPublisher publisher)
		{
			RegisterPublisher(publisher, null);
		}
		/// <summary>
		/// 向系统注册一个异常发布程序
		/// </summary>
		public static void RegisterPublisher(IExceptionPublisher publisher, NameValueCollection parameters)
		{
			if (null == publisher)
			{
				return;
			}

			publisher.Initialize(parameters);
			registeredPublishers.Add(publisher);
		}

		static List<IExceptionPublisher> registeredPublishers = new List<IExceptionPublisher>();
	}
} 
